/**
 * Created by csm on 2015/5/9.
 */
var ActivityManege = function(){
    var data = [];
    var CurrentA = '';
    return {
        init:function(){

            this.data();

        },
        data:function(){

            $.ajax({
                url:'http://api.tansuyun.cn?i=9GetJoinActivity&tsy='+getCookie('tsy'),
                data:{},
                method:'post',
                type:'json',
                success:function(d){
                    setCookie('tsy', d.tsy);
                    if(d.c == 200){
                        var html = '';
                        $.each(d.d,function(n,v){
                            html += "<tr id=\"ActID"+ v.ActID+"\">"
                                +"<td name='ActID' class='sorting_1 "+ v.ActID+"'>"+ v.ActID+"</td>"
                                +"<td name='ActivityName'  class='"+ v.ActID+"'>"+ v.ActivityName+"</td>"
                                +"<td name='Date'  class='"+ v.ActID+"'>"+ v.Date+"</td>"
                                +"<td name='Address'  class='"+ v.ActID+" center'>"+ v.Address+"</td>"
                                +"<td name='OrganizationID'  class='"+ v.ActID+" center'>"+ v.OrganizationID+"</td>"
                                +"<td name='Level'  class='"+ v.ActID+" center'>"+ v.Level+"</td>"
                                +"<td name='Year'  class='"+ v.ActID+" center'>"+ v.Year+"</td>"
                                +"<td name='Hour'  class='"+ v.ActID+" center'>"+ v.Hour+"</td>"
                                +"<td><a id='"+ v.ActID+"' class='edit'>编辑</a></td>"
                                +"<td><a  ActID = \""+v.ActID+"\" class='delete'>删除</a></td>"
                                +"<td><a  ActID = \""+v.ActID+"\"  class='add'>添加到学生活动记录表</a></td>"
                                +"</tr>";
                        });
                        $('#sample_editable_1 tbody').html(html);
                        $('.delete').click(function(){
                            var ActID = $(this).attr('ActID');//取得ActID值
//                            发起请求，开始操作
                            $.ajax({
                                url:'http://api.tansuyun.cn?i=9DelActivity&tsy='+getCookie('tsy'),
                                data:{ActID:ActID},
                                method:'post',
                                type:'json',
                                success: function (d) {
                                    if(d.c==200){
                                        $("#ActID"+ActID).remove();
                                    }
                                }
                            });
                        });
                        //暂未实现 保存
                        $('.edit').click(function(){
//                            切换状态及存储
                            if($(this).html() == '编辑'){
                                if(CurrentA!=''){
                                    alert('有未保存的更改');
                                    return;
                                }
                                //生成form表单
                                //$(this).parent().parent().prepend('<form id='+$(this).attr('id')+'>');
                                $.each($("."+$(this).attr('id')),function(i,v){
                                    var disable = $(v).attr('name')=='ActID'?"disabled":"";
                                    $(v).html("<input  style='width:100%' value='"+$(v).html()+"' name="+$(v).attr('name')+">");
                                });
                                $(this).html('保存');
                                //$(this).parent().parent().append('</form>');
                            }else{
                                CurrentA = this;
                                $.ajax({
                                    url:'http://api.tansuyun.cn?i=9ModifyActivity&tsy='+getCookie('tsy'),
                                    method:'post',
                                    type:'json',
                                    data: $("input").serialize(),
                                    success: function (d) {
                                        if(d.c==200){
                                            $.each($("."+$(CurrentA).attr('id')),function(i,v){
                                                var input = $(v).find('input');
                                                $(v).html(input.val());
                                            });
                                            $(CurrentA).html('编辑');
                                            CurrentA = '';
                                        }
                                    }
                                });
                            }
                        });
                        //添加跳转
                        $('.add').click(function(){
                            setCookie('ActID',$(this).attr('ActID'));
                            window.location.href='./A_Record-entering.html?ActID='+$(this).attr('ActID');
                        });

                    }
                },
                error:function(){

                }
            });
            $('#AddActivity').click(function(){
                 var html = '<tr id="ActID"><td name="ActID" class="sorting_1 1"><input style="width:100%" value="" name="ActID"></td><td name="ActivityName" class="1"><input style="width:100%" value="" name="ActivityName"></td><td name="Date" class="1"><input style="width:100%" value="" name="Date"></td><td name="Address" class="1 center"><input style="width:100%" value="" name="Address"></td><td name="OrganizationID" class="1 center"><input style="width:100%" value="" name="OrganizationID"></td><td name="Level" class="1 center"><input style="width:100%" value="" name="Level"></td><td name="Year" class="1 center"><input style="width:100%" value="" name="Year"></td><td name="Hour" class="1 center"><input style="width:100%" value="" name="Hour"></td><td><a id="NewActivity" class="">添加</a></td><td><a actid="1" class="delete">删除</a></td><td></td></tr>';
                $('#sample_editable_1 tbody').prepend(html);
                CurrentA = this;
                $('#NewActivity').click(function(){
                    $.ajax({
                        url:'http://api.tansuyun.cn?i=9AddActivity&tsy='+getCookie('tsy'),
                        method:'post',
                        type:'json',
                        data: $("input").serialize(),
                        success: function (d) {
                            if(d.c==200){
                               location.reload();
                            }
                        }
                    });
                });
            });

        },
        click:function(){
            $("#sample_editable_1_new").click(function(){
                var array = [{id:1}];
                $.each(array,function(i,v){
                    v.ActID = 'H'+ v.ActID;
                    $("#sample_editable_1").prepend("<tr>"
                    +"<td class='"+ v.ActID +" sorting_1'></td>"
                    +"<td class='"+ v.ActID+"'></td>"
                    +"<td class='"+ v.ActID+"'</td>"
                    +"<td class='"+ v.ActID+" center'></td>"
                    +"<td class='"+ v.ActID+" center'></td>"
                    +"<td class='"+ v.ActID+" center'></td>"
                    +"<td class='"+ v.ActID+" center'></td>"
                    +"<td class='"+ v.ActID+" center'></td>"
                    +"<td><a id='"+ v.ActID+"' class='edit'>编辑</a></td>"
                    +"<td><a class='delete'>删除</a></td>"
                    +"<td><a class='add' href='A_Record-entering.html'>添加到学生活动记录表</a></td>"
                    +"</tr>");
                });
            });
        },
    };
}();


